<?php

class Obj_DBTable_SearchIndex extends Zend_Db_Table_Abstract
{
    protected $_name = 'search_index';
    protected $_primary = 'ID';
    public $execTime = -1;
    
    
    public function write($id, $type, $content) {
    	$t = microtime(true);
    	$newid = $this->insert(
    		array(
    			'ContentID' => (int) $id,
    			'ContentType' => (int) $type,
    			'Content' => $content,
    			'created' => new Zend_Db_Expr('NOW()'),
    		)
    	);
    	$this->execTime = microtime(true) - $t;
    	return $newid;
    }
    
    public function search($phrase) {
    	$select = $this->select()->from($this, array(
    		'ContentID', 'ContentType', 'created', 'Content', 
    		'relev' => new Zend_Db_Expr("MATCH Content AGAINST ('".mysql_escape_string($phrase)."')")
    	));
    	$select->having('relev > 0')->order('relev DESC');
    	return $this->fetchAll($select)->toArray();
    }
}

